package com.rogo.rogobox.models
{
	import com.rogo.mvc.sub.proxy.Proxy;
	import com.core.microBlogs.qq.QQWeiboAPI;
	import com.core.microBlogs.qq.api.oauth.DoOauth;
	import com.core.microBlogs.standard.Oauth;
	import com.rogo.rogobox.App;
	import com.rogo.rogobox.AppFacade;
	import com.rogo.rogobox.models.data.Info;
	import com.rogo.rogobox.models.data.User;
	import com.rogo.rogobox.models.data.UserList;
	import com.rogo.rogobox.models.vo.FriendsReqVO;
	import com.rogo.rogobox.models.vo.TimelineVO;
	
	/**
	 * 1.10 账号接口
	 * 
	 * @see http://open.weibo.com/wiki/API文档
	 * 
	 * @author rogo
	 */	
	public class AccountProxy extends Proxy
	{
		public function AccountProxy()
		{
			super(NAME);
			
			init();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'accountProxy';
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		private var blog:QQWeiboAPI;
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		private function init():void
		{
			blog = new QQWeiboAPI(dataHandler, errorHandler);
		}

		/**
		 * 登录(暂只支持PIN)
		 */		
		public function login():void
		{
			blog.getRequestToken(App.APP_KEY, App.APP_SECRET, 'www.baidu.com');
		}
		
		/**
		 * 使用PIN码认证 
		 */		
		public function verify(pin:String):void
		{
			Oauth.oauthingKey.verify = pin;
			blog.getAccessToken();
		}
		
		public function getUserInfo():void
		{
			blog.getUserInfo('xml');
		}
		
		public function getFriendsTimeline(vo:TimelineVO):void
		{
			blog.getHomeTimeline(vo.pageFlag, vo.pageTime, vo.reqnum, vo.format);
		}
		
		public function getOtherInfo(name:String):void
		{
			blog.getOtherUserInfo(name, 'xml');
		}
		
		public function getUserTimeline(vo:TimelineVO):void
		{
			blog.getUserTimeline(vo.pageFlag, vo.pageTime, vo.reqnum, vo.lastId, vo.name, vo.format);
		}
		
		public function getFans(vo:FriendsReqVO):void
		{
			blog.getUserFansList(vo.name, vo.reqnum, vo.page, vo.format);
		}
		
		public function getFriends(vo:FriendsReqVO):void
		{
			blog.getUserIdolList(vo.name, vo.reqnum, vo.page, vo.format);
		}
		
		public function getMention(vo:TimelineVO):void
		{
			blog.getMentionsTimeline(vo.pageFlag, vo.pageTime, vo.reqnum, vo.lastId, vo.format);
		}
		
		public function publish(text:String):void
		{
			blog.addT(text, '127.0.0.1', null, null, 'xml');
		}
		
		public function repost(text:String, id:String):void
		{
			blog.reAddT(text, '127.0.0.1', id, null, null, 'xml');
		}
		
		public function commnet(text:String, id:String):void
		{
			blog.comment(text, '127.0.0.1', id, null, null, 'xml');
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function dataHandler(cmd:String, params:Object):void
		{
			switch (cmd)
			{
				case DoOauth.CMD_REQUEST_TOKEN:
					// 跳到授权页面
					blog.authRequestToken(); 
					break;
				case DoOauth.CMD_ACCESS_TOKEN:
					// 登录成功
					App.getInstance().verifyUser = params.name;
					App.getInstance().profilePageUser = params.name;
					App.getInstance().mainPageDisplay = params.name;
					App.getInstance().friendPageUser = params.name;
					sendNotification(AppFacade.LOGIN_RESULT);
					break;
				case 'CMD_USER_INFO':
					sendNotification(AppFacade.USER_PROFILE_RESULT, new User(params.data));
					break;
				case 'CMD_HOME_TIMELINE':
					sendNotification(AppFacade.FRIENDS_TIMELINE_RESULT, dataToInfo(params.data));
					break;
				case 'CMD_OTHER_USER_INFO':
					sendNotification(AppFacade.GET_OTHER_USER_INFO_RESULT, new User(params.data));
					break;
				case 'CMD_USER_TIMELINE':
					sendNotification(AppFacade.LOAD_USER_TIMELINE_RESULT, dataToInfo(params.data));
					break;
				case 'CMD_FRIENDS_USER_FANSLIST':
					sendNotification(AppFacade.LOAD_FOLLOWER_RESULT, dataToUser(params.data));
					break;
				case 'CMD_FRIENDS_USER_IDOLLIST':
					sendNotification(AppFacade.LOAD_FRIENDS_RESULT, dataToUser(params.data));
					break;
				case 'CMD_MENTIONS_TIMELINE':
					sendNotification(AppFacade.LOAD_MENTION_RESULT, dataToInfo(params.data));
					break;
				case 'CMD_ADD_T':
					sendNotification(AppFacade.PUBLISH_MICROBLOG_RESULT);
					break;
				case 'CMD_RE_ADD_T':
				    sendNotification(AppFacade.REPOST_MICROBLOG_RESULT);
					break;
			}
			
		}
		
		private function errorHandler(params:Object):void
		{
			trace('response error :' + params);
		}
		
		//--------------------------------------------------------------------------
		//
		//	Utils
		//
		//--------------------------------------------------------------------------
		
		private function dataToInfo(xml:XMLList):Array
		{
			var result:Array = new Array();
			for (var i:int = 0; i < xml.info.length(); i++)
			{
				result.push(new Info(xml.info[i]));
			}
			return result;
		}
		
		private function dataToUser(xml:XMLList):Array
		{
			var result:Array = new Array();
			for (var i:int = 0; i < xml.info.length(); i++)
			{
				result.push(new UserList(xml.info[i]));
			}
			return result;
		}
		
	}
}